<?php

class Marticle_attribute extends TST_Model {

    protected $table_article = "article";
     protected $table_article_attribute = "article_attribute";
    protected $selected_columns = "*";

    function Marticle_attribute() {
        parent::TST_Model();
    }

    public function getAllArticleByCategoriesID($language, $id, $status) {
        $sql = "SELECT *                                    
                    FROM {$this->table_article_attribute}
                    WHERE language = ? 
                    AND   status = ?
                    AND   category_id = ?
                    ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $status, $id));
    }

    public function getArticleByCategoriesID($language, $id, $article_url, $status) {
        $sql = "SELECT {$this->table_article_attribute}.*                                    
                    FROM {$this->table_article_attribute},{$this->table_article}
                    WHERE {$this->table_article_attribute}.language = ? 
                    AND   {$this->table_article_attribute}.status = ?
                    AND   {$this->table_article_attribute}.category_id = {$this->table_article}.id
                    AND   {$this->table_article_attribute}.friendly_url = ?
                    AND   {$this->table_article_attribute}.friendly_url = ?
                    ORDER BY {$this->table_article_attribute}.in_order, {$this->table_article}.name ";
        return $this->query_row($sql, array($language, $status, $id, $article_url));
    }
    
    public function getArticleByUrl1($language,$article_url,$status) {
        $sql = "SELECT {$this->table_article_attribute}.*                                    
                    FROM {$this->table_article_attribute}
                    WHERE {$this->table_article_attribute}.language = ? 
                    AND   {$this->table_article_attribute}.friendly_url = ?
                    AND   {$this->table_article_attribute}.status = ?";

        return $this->query_row($sql, array($language, $article_url,$status));
    }

    //for Admin -------------------------------------------------------------------------------------------
    
     public function getArticleByUrl($language,$article_url) {
        $sql = "SELECT {$this->table_article_attribute}.*                                    
                    FROM {$this->table_article_attribute}
                    WHERE {$this->table_article_attribute}.language = ? 
                    AND   {$this->table_article_attribute}.friendly_url = ?";

        return $this->query_row($sql, array($language, $article_url));
    }
    
    public function getAllArticleByCategoriesIDAd($language, $id) {
        $sql = "SELECT *                                    
                    FROM {$this->table_article_attribute}
                    WHERE language = ? 
                    AND   status NOT LIKE 'delete'
                    AND   category_id = ?
                    ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $id));
    }

    public function update($id, $data) {
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_article_attribute, $data, $where);
        return $this->db->query($sql);
    }

    public function addNew($data) {
        $this->db->insert($this->table_article_attribute, $data);
        return $this->db->insert_id();
    }

    public function delete($id) {
        $data = array(
            'status' => 'delete'
        );
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_article_attribute, $data, $where);
        return $this->db->query($sql);
    }

}
